import { defineStore } from 'pinia'
import {
  login,
  getUserInfo,
  getUserList,
  freezeUser,
  unfreezeUser,
  getCourseList,
  getCourseChapters,
  addCourse,
  updateCourse,
  deleteCourse,
  getChapterMaterials,
  addChapterMaterial,
  updateChapterMaterial,
  deleteChapterMaterial,
  addCourseChapter,
  getCourseStatistics,
  getCourseWatchCount
} from '@/api/admin'

export const useAdminStore = defineStore('admin', {
  state: () => ({
    userInfo: null,
    token: localStorage.getItem('token') || '',
    userList: [],
    courseList: [],
    courseChapters: [],
    chapterMaterials: [],
    courseStatistics: [],
    courseWatchCount: [],
    total: 0,
    currentPage: 1,
    pageSize: 10
  }),

  getters: {
    isLoggedIn: (state) => !!state.token
  },

  actions: {
    // 登录
    async login(credentials) {
      try {
        const res = await login(credentials)
        this.token = res.data.token
        localStorage.setItem('token', res.data.token)
        await this.fetchUserInfo()
        return res
      } catch (error) {
        console.error('Login failed:', error)
        throw error
      }
    },

    // 获取用户信息
    async fetchUserInfo() {
      try {
        const res = await getUserInfo()
        this.userInfo = res.data
        return res
      } catch (error) {
        console.error('Failed to fetch user info:', error)
        throw error
      }
    },

    // 退出登录
    logout() {
      this.token = ''
      this.userInfo = null
      localStorage.removeItem('token')
    },

    // 获取用户列表
    async getUserList(params) {
      try {
        const res = await getUserList(params)
        this.userList = res.data.items
        this.total = res.data.total
        this.currentPage = res.data.page
        return res.data
      } catch (error) {
        console.error('Failed to fetch user list:', error)
        throw error
      }
    },

    // 冻结用户
    async freezeUser(userId) {
      try {
        const res = await freezeUser(userId)
        return res
      } catch (error) {
        console.error('Failed to freeze user:', error)
        throw error
      }
    },

    // 解冻用户
    async unfreezeUser(userId) {
      try {
        const res = await unfreezeUser(userId)
        return res
      } catch (error) {
        console.error('Failed to unfreeze user:', error)
        throw error
      }
    },

    // 获取课程列表
    async getCourseList(params) {
      try {
        const res = await getCourseList(params)
        this.courseList = res.data.items
        this.total = res.data.total
        this.currentPage = res.data.page
        return res.data
      } catch (error) {
        console.error('Failed to fetch course list:', error)
        throw error
      }
    },

    // 获取课程章节
    async getCourseChapters(courseId) {
      try {
        const res = await getCourseChapters(courseId)
        this.courseChapters = res.data.chapters
        return res.data
      } catch (error) {
        console.error('Failed to fetch course chapters:', error)
        throw error
      }
    },

    // 创建课程
    async createCourse(data) {
      try {
        const res = await addCourse(data)
        return res
      } catch (error) {
        console.error('Failed to create course:', error)
        throw error
      }
    },

    // 更新课程
    async updateCourse(data) {
      try {
        const res = await updateCourse(data)
        return res
      } catch (error) {
        console.error('Failed to update course:', error)
        throw error
      }
    },

    // 删除课程
    async deleteCourse(courseId) {
      try {
        const res = await deleteCourse(courseId)
        return res
      } catch (error) {
        console.error('Failed to delete course:', error)
        throw error
      }
    },

    // 获取章节资料
    async getChapterMaterials(chapterId) {
      try {
        const res = await getChapterMaterials(chapterId)
        this.chapterMaterials = res.data.materials
        return res.data
      } catch (error) {
        console.error('Failed to fetch chapter materials:', error)
        throw error
      }
    },

    // 添加章节资料
    async addChapterMaterial(data) {
      try {
        const res = await addChapterMaterial(data)
        return res
      } catch (error) {
        console.error('Failed to add chapter material:', error)
        throw error
      }
    },

    // 更新章节资料
    async updateChapterMaterial(data) {
      try {
        const res = await updateChapterMaterial(data)
        return res
      } catch (error) {
        console.error('Failed to update chapter material:', error)
        throw error
      }
    },

    // 删除章节资料
    async deleteChapterMaterial(data) {
      try {
        const res = await deleteChapterMaterial(data)
        return res
      } catch (error) {
        console.error('Failed to delete chapter material:', error)
        throw error
      }
    },

    // 添加课程章节
    async addCourseChapter(data) {
      try {
        const res = await addCourseChapter(data)
        return res
      } catch (error) {
        console.error('Failed to add course chapter:', error)
        throw error
      }
    },

    // 获取课程统计信息
    async getCourseStatistics(params) {
      try {
        const res = await getCourseStatistics(params)
        this.courseStatistics = res.data.items
        this.total = res.data.total
        this.currentPage = res.data.page
        return res.data
      } catch (error) {
        console.error('Failed to fetch course statistics:', error)
        throw error
      }
    },

    // 获取课程观看次数
    async getCourseWatchCount(params) {
      try {
        const res = await getCourseWatchCount(params)
        this.courseWatchCount = res.data.items
        this.total = res.data.total
        this.currentPage = res.data.page
        return res.data
      } catch (error) {
        console.error('Failed to fetch course watch count:', error)
        throw error
      }
    }
  }
}) 